package com.woniu.controller;

import com.github.pagehelper.PageInfo;
import com.woniu.entity.FlowerInfo;
import com.woniu.service.FlowerInfoService;
import com.woniu.util.JwtUtil;
import com.woniu.util.R;
import com.woniu.vo.SortProductVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
public class ProductController {

    @Resource
    FlowerInfoService flowerInfoService;

    JwtUtil jwtUtil;

    @Autowired
    public void setJwtUtil(JwtUtil jwtUtil) {
        this.jwtUtil = jwtUtil;
    }

    @GetMapping("/selectAllByPage")
    public R getFlowerInfo(@RequestParam(defaultValue = "1") int pageNum,
                           @RequestParam(defaultValue = "5") int pageSize) {
        PageInfo<FlowerInfo> pageInfo = flowerInfoService.findAllFlowerInfo(pageNum, pageSize);
        return R.build(200, "数据请求成功", pageInfo);
    }

    @GetMapping("/checkFlowerInfoName/{flowerInfoName}")    //用于name查重
    public R checkFlowerInfoName(@PathVariable String flowerInfoName) {

        FlowerInfo flowerInfo = flowerInfoService.findFlowerInfoByFlowerInfoName(flowerInfoName);
        if (flowerInfo == null) {
            return R.build(200, "名称可以使用", null);
        }
        return R.error("鲜花已经存在！");
    }


    @GetMapping("/selectFlowerInfoName/{flowerInfoName}")
    public R selectFlowerInfoName(@PathVariable String flowerInfoName) {

        List<FlowerInfo> flowerInfos = flowerInfoService.selectFlowerInfoNameTo(flowerInfoName);
        if (flowerInfos == null) {
            return R.error("名称不存在");
        }
        return R.build(200, "查询成功！", flowerInfos);
    }

    @PostMapping("/selectFlowerInfoNameToPage")
    public R selectFlowerInfoNameToPage(@RequestBody SortProductVo sortProductVo) {
        List<FlowerInfo> flowerInfos = flowerInfoService.selectFlowerInfoNameToPage(sortProductVo);
        if (flowerInfos == null) {
            return R.error("名称不存在");
        }
        return R.build(200, "查询成功！", flowerInfos);
    }


    @PostMapping("/flowerInfo/addFlowerInfo")
    public R addFlowerInfo(FlowerInfo flowerInfo) {

        int i = flowerInfoService.addFlowerInfo(flowerInfo);
        if (i > 0) {
            return R.build(200, "新增鲜花成功", null);
        }
        return R.error("对不起，新增鲜花失败！！");
    }


    @PostMapping("/flowerInfo/editFlowerInfo")
    public R editFlowerInfo(FlowerInfo flowerInfo) {
        int i = flowerInfoService.editFlowerInfo(flowerInfo);
        if (i > 0) {
            return R.build(200, "修改鲜花成功", null);
        }
        return R.error("对不起，修改鲜花失败！！");
    }


    @PostMapping("/flowerInfo/deleteFlowerInfo/{id}")
    public R deleteFlowerInfo(@PathVariable int id) {
        int i = flowerInfoService.deleteFlowerInfo(id);
        if (i > 0) {
            return R.build(200, "删除鲜花成功", null);
        }
        return R.error("对不起，删除鲜花失败！！");
    }

    @PostMapping("/sort/sortFlowerInfo")
    public R sortFlowerInfoBySearchNameAndSortOrder(@RequestBody SortProductVo sortProductVo) {
        List<FlowerInfo> flowerInfos = flowerInfoService.sortFlowerInfoBySearchNameAndSortOrder(sortProductVo);
        if (flowerInfos == null) {
            return R.error("排序失败");
        }
        return R.build(200, "排序成功", flowerInfos);
    }


}
